<?php
include_once 'class/LogParser.acalss.inc';
include_once 'class/LogReader.aclass.inc';
class ProxyLogParser extends LogParser {
	private $pattern = '/(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) (?<nope1>-) (?<nope2>-) \[(?<date_time>(?<day>\d{2})\/(?<month>[[:alpha:]]+)\/(?<year>\d{4}):(?<time>\d{2}:\d{2}:\d{2})) (?<time_zone>(?:\+|-)\d{4})\] "(?<request>(?<method>\w+) (?<url>.+?) (?<protocol>\w+)\/(?<version>[\w\d\.]+))" (?<http_status>\d+) (?<content_length>\d+) (?<action>\w+):(?<hierarchy>\w+)/';
	private $urlPattern = '%((?:\w+://)?((?:(?:[[:alpha:]]\w*\.)+\w+)|(?:(?:\d{1,3}\.){3}\d{1,3}))|NONE://)%';
	
	protected function parser($log) {
		$matches = null;
		if(0 === preg_match($this->pattern, $log, $matches))
			return false;
		$url=$matches['url'];
		$url_matches = null;
		if(0 === preg_match($this->urlPattern, $url, $url_matches))
			return false;
		$matches['domain'] = $url_matches[1];
		$matches['month'] = monthNamed2Numbers($matches['month']);
		return $matches;
	}
	public function getDBVersion($log) {
		$res = array(
			'ip' => $log['ip'],
			'date' => $log['year']."-".$log['month']."-".$log['day']." ".$log['time'],
			'method' => $log['method'],
			'url' => $log['url'],
			'domain' => $log['domain'],
			'http_status' => $log['http_status'],
			'content_length' => $log['content_length'],
			'action' => $log['action'],
			'hierarchy' => $log['hierarchy']
			);
			
		return $res;
	}
	public function getName() {
		return 'proxy';
	}
}
?>